package com.hw

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

object Demo3Top10Student {
  def main(args: Array[String]): Unit = {
    /**
     * 3、统计总分排名前十学生各科的分数
     */
    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName("Filter")
    val sc = new SparkContext(conf)
    val scoreRDD: RDD[String] = sc.textFile("hadoop_code/src/data/score.txt")
    //取出总分前十的学生学号
    val top10AllScore: Array[String] = scoreRDD.map(
      line => {
        val split: Array[String] = line.split(",")
        (split(0), split(2).toInt)
      }
    ).reduceByKey((x, y) => x + y)
      .sortBy(kv => kv._2, false)
      .map{
        case (id:String, score:Int ) =>
          id
      }.take(10)

    //取出前十学生的各科分数
    scoreRDD.filter(
      line => top10AllScore.contains(line.split(",")(0))
    ).foreach(println)


  }

}
